package com.hibase.core.page.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.exception.HibaseException;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.page.entity.pagemenuContent.PageMenuContent;
import com.hibase.core.page.entity.pagemenuContent.vo.PageMenuContentInsertVO;
import com.hibase.core.page.entity.pagemenuContent.vo.PageMenuContentListVO;
import com.hibase.core.page.entity.pagemenuContent.vo.PageMenuContentSearchVO;
import com.hibase.core.page.entity.pagemenuContent.vo.PageMenuContentUpdateVO;
import com.hibase.core.page.entity.pagemenuContent.vo.PageMenuContentViewVO;
import com.hibase.core.page.service.pagemenucontent.PageMenuContentService;
import com.hibase.core.web.annotation.HibaseGetRequestMapping;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 菜单配置内容表 前端控制器
 * </p>
 *
 * @author junk
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/page-menu-content")
@Api(tags = "菜单配置内容表")
@SuppressWarnings({ "unchecked", "rawtypes" })
@Slf4j
public class PageMenuContentController{

    @Autowired
    private PageMenuContentService pageMenuContentService;

	@ApiOperation(value = "查询菜单配置内容表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectPageMenuContentList")
    public ResponseModel<PageMenuContentListVO> selectPageMenuContentList(@RequestBody PageMenuContentSearchVO searchVO) {

        PageInfo result = pageMenuContentService.selectPageMenuContentList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), PageMenuContentListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增菜单配置内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertPageMenuContent")
    public ResponseModel<String> insertPageMenuContent(@RequestBody PageMenuContentInsertVO insertVO) {

        PageMenuContent entity = EntityUtil.transform(insertVO, PageMenuContent.class);

        pageMenuContentService.savePageMenuContent(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改菜单配置内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updatePageMenuContent")
    public ResponseModel<String> updatePageMenuContent(@RequestBody PageMenuContentUpdateVO updateVO) {

        PageMenuContent entity = EntityUtil.transform(updateVO, PageMenuContent.class);

        pageMenuContentService.savePageMenuContent(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询菜单配置内容表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewPageMenuContentDetails")
    public ResponseModel<PageMenuContentViewVO> viewPageMenuContentDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(pageMenuContentService.viewPageMenuContent(operationidEntity.getId()), PageMenuContentViewVO.class));
    }

    @ApiOperation(value = "删除菜单配置内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deletePageMenuContent")
    public ResponseModel<String> deletePageMenuContent(@RequestBody OperationIdEntity delete) {

        pageMenuContentService.deletePageMenuContent(delete.getId());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "导出菜单配置内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/exportPageMenuContent")
    public ResponseModel<String> exportPageMenuContent(@RequestBody PageMenuContentSearchVO searchVO, HttpServletResponse response) {

        pageMenuContentService.exportPageMenuContent(searchVO, response);

        return ResponseModel.successMsg();
        }

    @ApiOperation(value = "同步xml配置到菜单",httpMethod = "POST")
    @HibasePostRequestMapping(value = "syncMenu")
    public ResponseModel syncMenu(){

        pageMenuContentService.syncMenuByXml();

        return ResponseModel.successMsg();
    }
    
    /**
	 * @Title: AttachmentPreviewController
	 * @Description: 下载PAGE-XML
	 * @author TimBrian
	 * @since 2021年11月5日 下午4:38:46
	 * @param attachmentHelpVO
	 * @return
	 */
	@ApiOperation(value = "下载PAGE-XML", httpMethod = "POST")
	@HibaseGetRequestMapping("/downloadPageXmlList")
	public void downloadPageXmlList(HttpServletResponse response, HttpServletRequest request) {
		try {
			pageMenuContentService.downloadPageXmlList(request, response);
		} catch (IOException e) {
			log.error(e.getMessage());
		} catch (HibaseException e) {
			log.error(e.getMessage());
		}
	}
}
